package com.hangzhou.xyj.manager;

import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import org.json.JSONObject;

import android.util.Log;

/**
 * Created by DT-038 on 2016/3/30.
 */
public class QiNiuManager {
    private static final String TAG = "QiNiuManager";
    private UploadManager mUploadManager;

    private QiNiuManager() {
    }

    public static final QiNiuManager getInstance() {
        return QiNiuManagerHolder.instance;
    }

    private void init() {
        Configuration config = new Configuration.Builder()
                .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认 256K
                .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认 512K
                .connectTimeout(10) // 链接超时。默认 10秒
                .responseTimeout(60) // 服务器响应超时。默认 60秒
//                .recorder(recorder)  // recorder 分片上传时，已上传片记录器。默认 null
//                .recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
//                .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
                .build();
        // 重用 uploadManager。一般地，只需要创建一个 uploadManager 对象
        mUploadManager = new UploadManager(config);
    }

    public void upload() {
        String token = "从服务端SDK获取";
        mUploadManager.put("Hello, World!".getBytes(), "hello", token,
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject response) {
                        Log.i("qiniu", info.toString());
                    }
                }, null);
    }

    private static class QiNiuManagerHolder {
        private static final QiNiuManager instance = new QiNiuManager();
    }
}
